package ntd.awt.tool;

import cn.hutool.core.util.ObjectUtil;
import ntd.util.UtilCatch;
import ntd.util.fun.Fun;

import javax.swing.*;
import java.awt.*;

public class ToolSwing {

    public static void run(Fun.EmptyFun emptyFun) {
        UtilCatch.tryCatchNoRes(() -> SwingUtilities.invokeLater(emptyFun::accept));
    }

    public static void add(Container container, Component component) {
        ToolSwing.run(() -> {
            container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
            container.add(component);
            container.revalidate();
        });
    }

    public static void initFlowLayout(Container container) {
        container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
    }


    public static JFrame initFrame() {
        return ToolSwing.initFrame("NTD", 1000, 1000);
    }

    public static JFrame initFrame(String title, Integer width, Integer height) {
//        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(ObjectUtil.defaultIfNull(width, 1000),
            ObjectUtil.defaultIfNull(height, 1000));
        jFrame.setTitle(ObjectUtil.defaultIfBlank(title, "NTD"));
        jFrame.setLocationRelativeTo(null);

        ToolSwing.initFlowLayout(jFrame);

        jFrame.setVisible(true);

        return jFrame;
    }

    public static JDialog initDialog(String content) {
        JDialog jDialog = new JDialog();

        jDialog.setSize(500, 500);

        jDialog.setLocationRelativeTo(null);
        jDialog.setTitle("弹窗");

        JTextArea jTextArea = new JTextArea();
        jTextArea.setText(content);
        jTextArea.setLineWrap(true);
        jTextArea.setWrapStyleWord(true);
        jTextArea.setColumns(200);
        jTextArea.setRows(20);

        JScrollPane jScrollPane = new JScrollPane(jTextArea,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        jDialog.add(jScrollPane);

        jDialog.setVisible(true);

        return jDialog;
    }

    public static void main(String[] args) {
        initDialog("123456");
    }
}
